var app = getApp();
const util = require('../../utils/util.js')
const date = require('../../utils/date.js')
const TxvContext = requirePlugin("tencentvideo");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    videoDetail: {},
    relatedVideosList: [],
    isCollection: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (app.globalData.isShare) {
      //从分享页面进来的话 等openid回调过来 在执行后面的方法
      app.porcessDataOpenid = res => {
        app.globalData.openid = res.data.openid;
        console.log('porcessDataOpenid playVideo openid:[' + app.globalData.openid + ']');
        let id = options.id;
        let data = {
          openid: app.globalData.openid,
          videoId: id
        }
        util.http(app.globalData.getVideoById + id, 'POST', data, this.porcessData);
        wx.showLoading();
        //由于全局只触发一次 
        app.globalData.isShare=false;
      }
    } else {
      console.log('playVideo openid:[' + app.globalData.openid + ']');
      let id = options.id;
      let data = {
        openid: app.globalData.openid,
        videoId: id
      }
      util.http(app.globalData.getVideoById + id, 'POST', data, this.porcessData);
      wx.showLoading();
    }
  },
  porcessData: function(res) {
    console.log(res);
    let relatedVideos = res.data.relatedVideos;
    let video = res.data.video;
    //inLook
    this.inLook(video.id);
    let videoDetail = {
      id: video.id,
      //categoryId: video.categoryId,
      duration: video.duration,
      hits: video.playCount,
      imgUrl: video.pic,
      title: video.title,
      vid: video.url.match(/page\/(\S*).html/)[1]
    }
    let relatedVideosList = [];
    for (let ids in relatedVideos) {
      let detail = relatedVideos[ids];
      var temp = {
        id: detail.id,
        title: detail.title,
        imgUrl: detail.pic,
        hits: detail.playCount,
        duration: detail.duration,
        vid: detail.url.match(/page\/(\S*).html/)[1]
      }
      relatedVideosList.push(temp);
    }

    this.setData({
      videoDetail: videoDetail,
      relatedVideosList: relatedVideosList,
      isCollection: res.data.isCollection
    });
    this._shareTitle();
    wx.setNavigationBarTitle({
      title: video.title,
    })
    wx.hideLoading();
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 300
    })

  },
  relatedVideosPlay: function(e) {
    let id = e.currentTarget.dataset.id;
    let data = {
      openid: app.globalData.openid,
      videoId: id
    }
    util.http(app.globalData.getVideoById + id, 'POST', data, this.porcessData);
    wx.showLoading();
  },

  _shareTitle: function() {
    let title = this.data.videoDetail.title;
    let name = [];
    let name1 = "超级好听的" + title + "，根本停不下来！";
    let name2 = "快来和我一起听" + title + "，从没见过这么好听的！";
    let name3 = "这首" + title + "听哭所有人，每天都要听一遍";
    let name4 = "只分享给和我关系最好的人，这首" + title + "一定要收藏！";
    name[0] = name1
    name[1] = name2
    name[2] = name3
    name[3] = name4
    this.setData({
      shareTitles: name
    })
  },
  /* 缓存收藏 getIsCollection: function(id){
     let videosCollected = wx.getStorageSync("_videosCollected");
     if (videosCollected) {
       let videoCollected = videosCollected[id];
       this.setData({
         isCollection: videoCollected
       })
     } else {
       videosCollected = [];
       videosCollected[id] = false;
       console.log(videosCollected);
       wx.setStorageSync("_videosCollected", videosCollected);
     }
   }, */

  inLook: function(videoId) {
    if (app.globalData.openid) {
      let data = {
        openid: app.globalData.openid,
        videoId: videoId,
        ztype: 2
      }
      util.http(app.globalData.userOperating, 'POST', data, null)
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },
  backindex: function() {
    wx.reLaunch({
      url: '/pages/index/index',
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(res) {
    console.log(res);
    if (res.from === 'button') {
      // 来自页面内转发按钮
    }
    return {
      title: this.data.shareTitles[Math.floor(Math.random() * this.data.shareTitles.length)],
      path: '/pages/playVideo/playVideo?id=' + this.data.videoDetail.id + '&openid=' + app.globalData.openid,
      imageUrl: this.data.videoDetail.imgUrl,
      success: function(res) {
        wx.showToast({
          title: '发送成功',
          icon: 'none'
        })
      },
      fail: function(res) {
        wx.showToast({
          title: '发送失败',
          icon: 'none'
        })
      }
    }

  }

})